package
{
	import com.greensock.plugins.DropShadowFilterPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import gameSocketer.Socketer;
	import gameSocketer.socketParsers.CardGameSocketPaser;
	import gameSocketer.socketParsers.ChipSocketPaser;
	import gameSocketer.socketParsers.LoginSocketPaser;
	import gameSocketer.socketSenders.CardGameSocketSender;
	import gameSocketer.socketSenders.ChipSocketSender;
	import gameSocketer.socketSenders.LoginSocketSender;
	
	import gameUi.ChipBox;
	import gameUi.Event.LoadingEvent;
	import gameUi.Event.LoginEvent;
	import gameUi.GameUi;
	import gameUi.LoadingUI;
	import gameUi.LoginUI;
	import gameUi.SoundManager;
	
	import modle.Role;
	
	public class BlackJack extends Sprite
	{
		public static var _stage:Stage;//舞台
		
		public static var role:Role;//角色对象
		
		public static var _gameUi:GameUi;//游戏主场景
		
		public static var socketer:Socketer;//通信
		
		public static var eventTower:EventDispatcher;//事件派发器
		
		public static var chipBox:ChipBox;//筹码选择栏
		
		public static var loginUI:LoginUI; //测试登录界面
		
		public static var loginPaser:LoginSocketPaser;//登录包分析
		public static var loginSender:LoginSocketSender;//登录包发送
		
		public static var cardGameSocketPaser:CardGameSocketPaser;//游戏逻辑包分析
		public static var cardGameSocketSender:CardGameSocketSender;//游戏逻辑包发送
		
		public static var chipSocketPaser:ChipSocketPaser;//筹码兑换包分析
		public static var chipSocketSender:ChipSocketSender;//筹码兑换包发送
		
		public static var chipMin:Number;//最最小注额
		public static var chipMax:Number;//最大注额
		
		private var my_URLLoader:URLLoader;
		
		public function BlackJack()
		{
			_stage=stage;
			stage.align= StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			TweenPlugin.activate([DropShadowFilterPlugin]); 
			
			eventTower = new EventDispatcher();
			
			role = new Role();
			
			socketProLoader();
		}
		
		
		
		private function socketProLoader():void{
			var my_URLRequest:URLRequest = new URLRequest("../swc/ipConfig.xml");
			my_URLLoader = new URLLoader();
			my_URLLoader.load(my_URLRequest);
			// var my_Loader:Loader = new Loader();
			my_URLLoader.addEventListener(Event.COMPLETE , socketLoadOver);
		}
		
		private function socketLoadOver(e:Event):void{
			var my_xml:XML = new XML(my_URLLoader.data);
			trace("请求连接socket：",my_xml.ip. @ host,my_xml.ip. @ port);
			socketer = new Socketer(my_xml.ip. @ host,my_xml.ip. @ port);
			
			chipMin = Number(my_xml.chip. @ min);
			chipMax = Number(my_xml.chip. @ max);
			initSocket();
			
			loginUI = new LoginUI();
			addChild(loginUI);
			
			_gameUi = new GameUi();
			//初始化筹码选择栏
			chipBox = new ChipBox();
			
			eventTower.addEventListener(LoginEvent.LOGIN_SUCCESS,showGameUI);
			
			SoundManager.instance.playBackground();
		}
		
		private function initSocket():void{
			loginPaser = new LoginSocketPaser();
			loginSender = LoginSocketSender.getInstance();
			cardGameSocketPaser = new CardGameSocketPaser();
			cardGameSocketSender = CardGameSocketSender.getInstance();
			chipSocketPaser = new ChipSocketPaser();
			chipSocketSender = ChipSocketSender.getInstance();
		}
		
		public function showGameUI(e:LoginEvent=null):void{
			if(contains(loginUI)) removeChild(loginUI);
			addChild(_gameUi);
			_gameUi.initGame();
		}
	}
}